# -*- coding: utf-8 -*-
# @Time : 2024/1/20 20:38
# @Author : cute
# @Email : Aggressive_cute@126.com
import functools
from sanic.response import text
from sanic.request import Request
from sanic import Blueprint, json
from .xhs.new_xhs_slide import XhsCaptcha
from settings.server_settings import white_list

xhs_api = Blueprint('xhs_api', url_prefix='/cute_api/xhs_api')


# 验签
def verify_signature(func):
    invalid_info = 'Invalid Signature 请联系管理员(v):  Python_Cute2023'
    @functools.wraps(func)
    def wrapper(*args, **kw):
        request = args[0]
        signature = request.headers.get('signature')
        if not signature:
            return text(invalid_info, status=403)

        if signature not in white_list:  # 白名单
            return text(invalid_info, status=403)

        return func(*args, **kw)

    return wrapper


# /cute_api/xhs_api/slide
@xhs_api.route("/slide", methods=["POST", "GET"])
@verify_signature
async def xhs_slide(request):
    """POST JSON{fp/ detail}"""
    data_dict = None
    if request.method != "POST":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
        return json(data_dict)
    try:
        client_dict = request.json

        verify_uuid = client_dict.get('verify_uuid')

        if verify_uuid:
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": XhsCaptcha(**client_dict).pass_slide()

            }
        else:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": "请传入完整参数"
            }
            return json(data_dict)


    except Exception as e:
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": str(e)
        }

    return json(data_dict)


